package cn.iocoder.an.module.business.controller.admin.equipmentkeeptype;

import cn.iocoder.an.module.business.controller.admin.equipmentkeepsetting.vo.EquipmentSettingImportExcelVO;
import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import cn.iocoder.an.module.business.enums.MaintenanceStatusEnum;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.equipmentkeeptype.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmentkeeptype.EquipmentKeepTypeDO;
import cn.iocoder.an.module.business.service.equipmentkeeptype.EquipmentKeepTypeService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 设备保养分类")
@RestController
@RequestMapping("/business/equipment-keep-type")
@Validated
public class EquipmentKeepTypeController {

    @Resource
    private EquipmentKeepTypeService equipmentKeepTypeService;


    @PostMapping("/create")
    @Operation(summary = "创建设备保养分类")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-type:create')")
    public CommonResult<Long> createEquipmentKeepType(@Valid @RequestBody EquipmentKeepTypeSaveReqVO createReqVO) {
        return success(equipmentKeepTypeService.createEquipmentKeepType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备保养分类")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-type:update')")
    public CommonResult<Boolean> updateEquipmentKeepType(@Valid @RequestBody EquipmentKeepTypeSaveReqVO updateReqVO) {
        equipmentKeepTypeService.updateEquipmentKeepType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备保养分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-type:delete')")
    public CommonResult<Boolean> deleteEquipmentKeepType(@RequestParam("id") Long id) {
        equipmentKeepTypeService.deleteEquipmentKeepType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备保养分类")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-type:delete')")
    public CommonResult<Boolean> deleteEquipmentKeepTypeList(@RequestParam("ids") List<Long> ids) {
        equipmentKeepTypeService.deleteEquipmentKeepTypeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备保养分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-type:query')")
    public CommonResult<EquipmentKeepTypeRespVO> getEquipmentKeepType(@RequestParam("id") Long id) {
        EquipmentKeepTypeDO equipmentKeepType = equipmentKeepTypeService.getEquipmentKeepType(id);
        return success(BeanUtils.toBean(equipmentKeepType, EquipmentKeepTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备保养分类分页")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-type:query')")
    public CommonResult<PageResult<EquipmentKeepTypeRespVO>> getEquipmentKeepTypePage(@Valid EquipmentKeepTypePageReqVO pageReqVO) {
        PageResult<EquipmentKeepTypeDO> pageResult = equipmentKeepTypeService.getEquipmentKeepTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentKeepTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备保养分类分页")
    public CommonResult<List<EquipmentKeepTypeRespVO>> getEquipmentKeepTypeList() {
        List<EquipmentKeepTypeDO> list = equipmentKeepTypeService.getEquipmentKeepTypeList();
        return success(BeanUtils.toBean(list, EquipmentKeepTypeRespVO.class));
    }

    @GetMapping("/listNew")
    public CommonResult<List<EquipmentKeepTypeRespVO>> getEquipmentKeepTypeListNew() {
        List<EquipmentKeepTypeDO> list = equipmentKeepTypeService.getEquipmentKeepTypeListNew();
        return success(BeanUtils.toBean(list, EquipmentKeepTypeRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出设备保养分类 Excel")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentKeepTypeExcel(@Valid EquipmentKeepTypePageReqVO pageReqVO,
                                             HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentKeepTypeDO> list = equipmentKeepTypeService.getEquipmentKeepTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备保养分类.xls", "数据", EquipmentKeepTypeRespVO.class,
                BeanUtils.toBean(list, EquipmentKeepTypeRespVO.class));
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得保养设置导入模板导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<EquipmentKeepTypeImportExcelVO> list = Arrays.asList(
                EquipmentKeepTypeImportExcelVO.builder().name("电阻焊接设备")
                        .status(MaintenanceStatusEnum.run.getStatus()).description("xxxxx描述").build()
        );
        // 输出
        ExcelUtils.write(response, "保养分类导入模板.xlsx", "列表", EquipmentKeepTypeImportExcelVO.class, list);
    }


    @PostMapping("/import")
    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<EquipmentKeepTypeImportExcelVO> list = ExcelUtils.read(file, EquipmentKeepTypeImportExcelVO.class);
        return success(equipmentKeepTypeService.importList(list, updateSupport));
    }

}